package com.pzh.guli.service.edu.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.edu.feign.OssFileService;
import com.pzh.guli.service.edu.pojo.Teacher;
import com.pzh.guli.service.edu.mapper.TeacherMapper;
import com.pzh.guli.service.edu.pojo.vo.TeacherVo;
import com.pzh.guli.service.edu.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Autowired
    private OssFileService ossFileService;
    @Override
    public IPage selectPage(Page<Teacher> pageParam, TeacherVo teacherVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("sort");
        if (teacherVo == null) {
            return teacherMapper.selectPage(pageParam, queryWrapper);
        }
        String name = teacherVo.getName();
        Integer level = teacherVo.getLevel();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        return teacherMapper.selectPage(pageParam, queryWrapper);
    }
    @Override
    public List<Map<String, Object>> getNameMapList(String key) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight("name",key);
        List<Map<String, Object>> list = teacherMapper.selectMaps(queryWrapper);
        return list;
    }

    @Override
    public boolean removeTar(String id) {
        Teacher teacher = teacherMapper.selectById(id);
        if (teacher==null){
            return false;
        }
        else {
            String avatar = teacher.getAvatar();
            Result result = ossFileService.removeFileByString(avatar);
            if (result.getCode()==20000){//删除成功
                return true;
            }
            else {
                return false;
            }
        }
    }
    //前台热门老师展示 top 4
    @Cacheable(value = "index",key = "'getTopTeacher'") //redis的使用
    @Override
    public List<Teacher> getTopTeacher() {
        QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("sort");
        teacherQueryWrapper.last("limit 4");
        return teacherMapper.selectList(teacherQueryWrapper);
    }
}
